package gold.digger;

import javafx.util.Pair;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC423 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        /**
         * 0 zero   -z  1
         * 1 one    -o  5
         * 2 two    -w  1
         * 3 three  -r  2
         * 4 four   -u  1
         * 5 five   -v  4
         * 6 six    -x  1
         * 7 seven  -s  3
         * 8 eight  -g  1
         * 9 nine   -i  6
         *
         * <p>
         * 输入: "owoztneoer"
         * <p>
         * 输出: "012" (zero one two)
         *
         * @param s
         * @return
         */

        public String originalDigits(String s) {
            int[] dict = new int[26];
            for (char ch : s.toCharArray()) {
                dict[ch - 'a']++;
            }

            int[] count = new int[10];
            Map<Integer, Pair<Character, String>> table = new LinkedHashMap<>();
            table.put(0, new Pair<>('z', "zero"));
            table.put(2, new Pair<>('w', "two"));
            table.put(4, new Pair<>('u', "four"));
            table.put(6, new Pair<>('x', "six"));
            table.put(8, new Pair<>('g', "eight"));

            table.put(3, new Pair<>('r', "three"));
            table.put(7, new Pair<>('s', "seven"));
            table.put(5, new Pair<>('v', "five"));
            table.put(1, new Pair<>('o', "one"));
            table.put(9, new Pair<>('i', "nine"));

            for (Integer num : table.keySet()) {
                count[num] = dict[table.get(num).getKey() - 'a'];
                for (char ch : table.get(num).getValue().toCharArray()) {
                    if (dict[ch - 'a'] - count[num] >= 0) dict[ch - 'a'] -= count[num];
                }
            }

            StringBuilder res = new StringBuilder();
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < count[i]; j++) {
                    res.append(String.valueOf(i));
                }
            }

            return res.toString();
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.originalDigits("fviefuro"));
    }

    public static void main(String[] args) throws Exception {
        LC423 an = new LC423();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
